package com.example.ex5_springboot.controller;

import com.example.ex5_springboot.Result;
import com.example.ex5_springboot.pojo.AdminGetPerson;
import com.example.ex5_springboot.pojo.UserRegister;
import com.example.ex5_springboot.services.UserRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@CrossOrigin
public class UserRegisterController {

    @Autowired
    UserRegisterService userRegisterService;

    @GetMapping("/getUserUnregistered")
    public Result getUserUnregistered(){
        try {
            List<UserRegister> userRegisters = userRegisterService.getAllUnRegisterUser();
            for (int i = 0;i < userRegisters.size();i++){
                int count = userRegisterService.getCountOfUnregistered(userRegisters.get(i).getUser_id());
                userRegisters.get(i).setUnregistered(count);
            }
            return Result.success(userRegisters);
        } catch (Exception e) {
            return Result.fail();
        }
    }

    @GetMapping("/getUnRegisterUserByName")
    public Result getUnRegisterUserByName(@RequestParam("name") String name) {
        Integer id = 0;
        try {
            id = Integer.parseInt(name);
        } catch (NumberFormatException e) {
            return  Result.fail();
        } catch (Exception e) {
            return  Result.fail();
        }
        try {
            List<UserRegister> userRegisters =userRegisterService.getUnRegisterUserByName(id);
            for (int i = 0;i < userRegisters.size();i++){
                int count = userRegisterService.getCountOfUnregistered(userRegisters.get(i).getUser_id());
                userRegisters.get(i).setUnregistered(count);
            }
            return Result.success(userRegisters);
        } catch (Exception e) {
            return Result.fail();
        }
    }
}
